package com.zju.DoctorGo.controller;

import com.zju.DoctorGo.dto.UserEditPasswordDTO;
import com.zju.DoctorGo.dto.UserUnregisterDTO;
import com.zju.DoctorGo.entity.Doctor;
import com.zju.DoctorGo.entity.Patient;
import com.zju.DoctorGo.entity.Result;
import com.zju.DoctorGo.entity.User;
import com.zju.DoctorGo.dto.UserLoginDTO;
import com.zju.DoctorGo.mapper.PatientMapper;
import com.zju.DoctorGo.service.DoctorYellowPageService;
import com.zju.DoctorGo.service.PatientService;
import com.zju.DoctorGo.vo.UserLoginVO;
import com.zju.DoctorGo.service.UserService;
import com.zju.DoctorGo.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@RestController
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private DoctorYellowPageService doctorService;

    @Autowired
    private PatientService patientService;

    @Autowired
    private PatientMapper patientMapper;




    @PostMapping("/api/user/login")
    public Result login(@RequestBody UserLoginDTO userLoginDTO) {
        log.info("员工登录:{}",userLoginDTO);
        User u=userService.login(userLoginDTO);
        //登录成功，返回token
        Map<String,Object> claims=new HashMap<>();
        claims.put("userId",u.getUserId());
        claims.put("userName",u.getUserName());
        String jwt= JwtUtils.generateJwt(claims);
        UserLoginVO userLoginVO=UserLoginVO
                .builder()
                .userId(u.getUserId())
                .userName(u.getUserName())
                .userType(u.getUserType())
                .token(jwt)
                .build();
        return Result.success(userLoginVO);
    }
    @PutMapping("/api/user/editPassword")
    public Result editPassword(@RequestBody UserEditPasswordDTO userEditPasswordDTO) {
        log.info("修改密码:{}",userEditPasswordDTO);
        userService.editPassword(userEditPasswordDTO);
        return Result.success();
    }

    @DeleteMapping("/api/user/unregister")
    public Result unregister(@RequestBody UserUnregisterDTO userUnregisterDTO) {
        log.info("注销账号:{}",userUnregisterDTO);
        userService.unregister(userUnregisterDTO);
        return Result.success();
    }

    @GetMapping("/api/user/{userId}")
    public Result getUserById(@PathVariable("userId") Integer userId) {
        User user=userService.getById(userId);
        if(user!=null)
            return Result.success(user);
        return Result.error("找不到用户");
    }

    @GetMapping("/api/user/username/{userName}")
    public Result getUserByName(@PathVariable("userName") String userName) {
        User user=userService.getByUserName(userName);
        if(user!=null)
            return Result.success(user);
        return Result.error("找不到用户");
    }

    @GetMapping("/api/user/verifyCode")
    public Result getVerifyCode(String targetEmailBox) {
        Map<String,Object> resultMap=userService.getVerifyCode(targetEmailBox);
        return Result.success(resultMap);
    }

    @PostMapping("/api/user/verifyCode/{code}")
    public Result verifyCode(@RequestBody Map<String,Object> requestMap,@PathVariable("code") String code) {
        userService.veryfyCode(requestMap,code);
        return Result.success();
    }

    @PutMapping("/updatePatient")
    public Result updatePatient(@RequestBody Patient patient) {
        patientMapper.update(patient);
        return Result.success();
    }

    @GetMapping("/getPatientById")
    public Result getPatientById(@RequestParam("id") Integer id){
        Patient patient=patientService.getById(id);
        if(patient!=null)
            return Result.success(patient);
        return Result.error("找不到患者");
    }

    @PostMapping("/api/user/register")
    public Result register(@RequestBody User user) {
        userService.register(user);

        return Result.success();
    }
}